package com.mbc.hr.manager;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.HashMap;
import java.util.Map;

import net.sf.jasperreports.engine.JRResultSetDataSource;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.util.JRLoader;

import com.mbc.common.CommonConstants.RecordStatus;
import com.mbc.common.manager.AbstractMgr;
import com.mbc.hr.param.EmployeeReportCriteria;

public class EmployeeMgmtReportMgr extends AbstractMgr {
	public EmployeeMgmtReportMgr(){}
	
	public EmployeeMgmtReportMgr(String sessionId){
		this.sessionId = sessionId;
	}
	
	public JasperPrint prepareEmployeeList(EmployeeReportCriteria cri, String rptFilePath) throws Exception {
		openJDBCConnection();
		JasperPrint l_jsPrint = null;
		try {
			PreparedStatement l_pstmt = getConnection().prepareStatement("call sp_rpt_employeelist(?,?,?,?,?,?)");
			int i = 1;
			l_pstmt.setLong(i++, cri.getDesignationID());
			l_pstmt.setLong(i++, cri.getDepartmentID());
			l_pstmt.setLong(i++, cri.getBranchId());
			l_pstmt.setInt(i++, cri.getFromSY());
			l_pstmt.setInt(i++, cri.getToSY());
			l_pstmt.setByte(i++, RecordStatus.active);
			ResultSet l_rs = l_pstmt.executeQuery();
			JRResultSetDataSource l_jrRS = new JRResultSetDataSource(l_rs);
			JasperReport l_jsRpt =(JasperReport)JRLoader.loadObjectFromFile(rptFilePath);
			
			Map<String, Object> l_params = new HashMap<String, Object>();
			l_params.put("Branch", cri.getBranchId() == 0 ? "" : cri.getBranchName());
			
			l_jsPrint = JasperFillManager.fillReport(l_jsRpt, l_params, l_jrRS);
		} catch (Exception e) {
			throw e;
		} finally {
			closeJDBCConnection();
		}

		return l_jsPrint;

	}
}
